// 时间显示多少分钟前
function friendlyFormatTime(stringTime) {
  let str = stringTime;
  //将字符串转换成时间格式 console.log(this.friendlyFormatTime('2021-12-22 17:42:09'));
  let timePublish = new Date(str);
  let timeNow = new Date();
  let minute = 1000 * 60;
  let hour = minute * 60;
  let day = hour * 24;
  let month = day * 30;
  let year = month * 12;
  let diffValue = timeNow - timePublish;
  let diffMonth = diffValue / month;
  let diffWeek = diffValue / (7 * day);
  let diffDay = diffValue / day;
  let diffHour = diffValue / hour;
  let diffMinute = diffValue / minute;
  let diffYear = diffValue / year;
  let result = null;
  if (diffValue < 0) {
    result = "刚刚";
  } else if (diffYear > 1) {
    result = parseInt(diffYear) + "年前";
  } else if (diffMonth > 1) {
    result = parseInt(diffMonth) + "月前";
  } else if (diffWeek > 1) {
    result = parseInt(diffWeek) + "周前";
  } else if (diffDay > 1) {
    result = parseInt(diffDay) + "天前";
  } else if (diffHour > 1) {
    result = parseInt(diffHour) + "小时前";
  } else if (diffMinute > 1) {
    result = parseInt(diffMinute) + "分钟前";
  } else {
    result = "刚刚";
  }
  return result;
}

// 时间判断今天 昨天 几天前
function judgeTime(date) {
  let dateStr = new Date(date.replace(/-/g, "/"));
  let today = new Date();
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);
  today.setMilliseconds(0);
  let otime = today.getTime();
  // 给出时间 - 今天0点
  let offset = dateStr.getTime() - otime;
  let isToday = offset / 1000 / 60 / 60;
  if (isToday > 0 && isToday <= 24) {
    return "今天";
  } else if (isToday < 0 && isToday >= -24) {
    return "昨天";
  } else {
    return date.substring(0, 10); //只留年月日
  }
}

/**
 * 导出
 **/
export {
  friendlyFormatTime,
  judgeTime
};
